/***********************************************************
 *  File_Name  : XJJsonBase.h
 *  File_Path   :
 *  File_Encode : UTF8
 *  Description : Json序列化基类
 *
 *  OS : Linux, UNIX
 *  LastVersion  : 20240213
 *
 *  Author      : LiXiujie(李秀杰)
 *  Date        : 2024-2-13
 *  Version     : 20240213
 *  Description :
 *
 *  Edit Author :
 *  Edit Date   :
 *  Version     :
 *  Description :
 *
 ***********************************************************/
#ifndef XJJSONBASE_H_
#define XJJSONBASE_H_

#include <list>
#include <string>
#include <cjson/cJSON.h>

namespace XJUtilClass
{
    #define DEF_FUN_NEW(T) \
        virtual XJJsonBase *New() { return new T(); }

    class XJJsonBase;
    // 支持字段类型
    enum XJJFTYPE
    {
        XJJFT_STRING = 0,    // std::string
        XJJFT_UINT16,        // uint16_t
        XJJFT_UINT32,        // uint32_t
        XJJFT_UINT64,        // uint64_t
        XJJFT_INT16,         // int16_t
        XJJFT_INT32,         // int32_t
        XJJFT_INT64,         // int64_t
        XJJFT_FLOAT,         // float
        XJJFT_DOUBLE,        // double
        XJJFT_OBJ,           // object
        XJJFT_LIST_OBJ,      // list<object>
        XJJFT_LIST_STR,      // list<string>
        XJJFT_END
    };
    enum XJJRET {
        XJJ_RET_ERR = -1,
        XJJ_RET_OK = 0,
    };
    class XJJsonField
    {
    public:
        std::string _name;
        int _type;           // XJFTYPE
        void *_value;
        XJJsonBase *_val_type;
        XJJsonField();
        XJJsonField(const std::string &name, const int type, void *value, XJJsonBase *val_type = NULL);
        virtual ~XJJsonField();
    };

    class XJJsonBase
    {
    public:
        static bool UseUTF8;

    protected:
        std::list<XJJsonField *> _bind_map;

    public:
        virtual cJSON *PackObj() const;
        // 返回值: NET_RET_OK:成功; 其他失败
        virtual int UnPack(cJSON *cjson);
        virtual std::string Pack() const;
        // 返回值: 小于0:失败;大于等于0:已解析索引
        virtual int UnPack(uint8_t *data, int index, int count);
        // 返回值: 小于0:失败;大于等于0:已解析索引
        virtual int UnPack(std::string data);
        DEF_FUN_NEW(XJJsonBase);
        XJJsonBase();
        virtual ~XJJsonBase();
        XJJsonBase &operator=(const XJJsonBase &other)
        {
            return *this;
        }
    };

    #define BIND_OBJ_FIELD(name, type) _bind_map.push_back(new XJUtilClass::XJJsonField(#name, type, (void *)&name));
    #define BIND_OBJ_FIELD_3(name, val, type) _bind_map.push_back(new XJUtilClass::XJJsonField(name, type, (void *)&val));
    #define BIND_ARR_FIELD(name, type, item_type) _bind_map.push_back(new XJUtilClass::XJJsonField(#name, type, (void *)&name, (XJJsonBase *)(item_type)));

} // XJUtilClass
#endif // XJJSONBASE_H_
